home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 026-050 / scopedisk35 / mousezoom / mousezoom.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  6KB  |  235 lines

  1. #define TASKNAME    "Mouse Zoom"
  2. #define TERMINATE    0xff
  3.  
  4. #include <exec/types.h>
  5. #include <exec/lists.h>
  6. #include <exec/memory.h>
  7. #include <exec/interrupts.h>
  8. #include <exec/libraries.h>
  9. #include <exec/io.h>
  10. #include <exec/tasks.h>
  11. #include <exec/devices.h>
  12. #include <devices/input.h>
  13. #include <devices/inputevent.h>
  14. #include <intuition/intuition.h>
  15. #include <libraries/dos.h>
  16. #include <graphics/gfxmacros.h>
  17.  
  18. #include <proto/dos.h>
  19. #include <proto/exec.h>
  20. #include <proto/intuition.h>
  21. #include <proto/graphics.h>
  22. #include <string.h>
  23.  
  24. #define abs(x) ((x) < 0 ? -(x) : (x))
  25.  
  26. struct MsgPort *FindPort(), *CreatePort();
  27. void DeletePort();
  28.  
  29. struct OURMSG {
  30.  struct Message msgpart;
  31.  short  Command;
  32.  short  Misc;
  33.  };
  34.  
  35. /* Declarations for CBACK */
  36. extern BPTR _Backstdout;         /* standard output when run in background */
  37. long _BackGroundIO = 0;          /* Flag to tell it we don't want to do I/O      */
  38. long _stack = 4000;              /* Amount of stack space our task needs   */
  39. char *_procname = "Mouse Zoom";  /* The name of the task to create         */
  40. long _priority = 0;              /* The priority to run us at              */
  41.  
  42. /************************************************************************/
  43. /* the handler subroutine - called through the handler stub             */
  44. /************************************************************************/
  45. struct InputEvent *myhandler(ev, gptr)
  46. struct InputEvent *ev;            /* and a pointer to a list of events */
  47. APTR   gptr;                      /* Everything we need to know about */
  48. {
  49. register struct InputEvent *ep;
  50. register short i;
  51.  
  52.    /* run down the list of events to see if they are mouse-move */
  53.  
  54. for (ep = ev; ep != NULL; ep = ep->ie_NextEvent)
  55.     {
  56.     if ((ep->ie_Class == IECLASS_RAWMOUSE)    &&
  57.         (ep->ie_Qualifier & IEQUALIFIER_RELATIVEMOUSE))
  58.         {
  59.     i = abs(ep->ie_X) >> 3;        /* adjust x value */
  60.     if (i != 0) ep->ie_X *= i;
  61.     i = abs(ep->ie_Y) >> 3;        /* adjust y value */
  62.     if (i != 0) ep->ie_Y *= i;
  63.         }
  64.      }
  65.    /* pass on the pointer to the event */
  66. return(ev);
  67. }
  68.  
  69. /* * * * * * * * * * * EXTERNAL ROUTINES * * * * * * * * * */
  70.  
  71. extern struct MsgPort  *CreatePort();
  72. struct IOStdReq  *CreateIOReq(struct MsgPort *, int);
  73. void              DeleteIOReq(struct IOStdReq *);
  74. extern void       HandlerInterface();
  75.  
  76. /************************************************************************/
  77. /* the main program to do the popcli stuff                              */
  78. /************************************************************************/
  79. void _main(cmd)
  80. char *cmd;
  81. {
  82. struct OURMSG *msg;
  83. short Command;
  84. short Misc;
  85. struct MsgPort     *port;
  86. struct MsgPort     *inputDevPort;
  87. struct IOStdReq    *inputRequestBlock;
  88. struct Interrupt      handlerStuff;
  89.  
  90. port                = NULL;
  91. inputDevPort        = NULL;
  92. inputRequestBlock   = NULL;
  93.  
  94.     /* see if we already exist, and if so, wake up and die */
  95.  
  96. if ((port = FindPort(TASKNAME)) != NULL)
  97.     {
  98.     if ((msg = (struct OURMSG *)
  99.               AllocMem(sizeof(struct OURMSG), MEMF_CLEAR|MEMF_PUBLIC)) == NULL)
  100.     goto abort;
  101.     msg->Command = TERMINATE;
  102.     msg->msgpart.mn_Node.ln_Type = NT_MESSAGE;
  103.     msg->msgpart.mn_Node.ln_Pri  = 0;
  104.     msg->msgpart.mn_Length = sizeof(struct OURMSG);
  105.     PutMsg(port,msg);
  106.     port = NULL;
  107.     goto abort;
  108.     }        
  109.    
  110.    
  111. if ((port  = CreatePort(TASKNAME,0)) == NULL) goto abort;
  112.  
  113. if (  ((inputDevPort = CreatePort(0,0)) == NULL)                        ||
  114.  
  115.       ((inputRequestBlock =
  116.           CreateIOReq(inputDevPort, sizeof(struct IOStdReq))) == NULL)  ||
  117.  
  118.       OpenDevice("input.device",0,(struct IORequest *)inputRequestBlock,0))
  119.  
  120.       goto abort;
  121.  
  122. handlerStuff.is_Data = NULL;
  123. handlerStuff.is_Code = HandlerInterface;
  124. handlerStuff.is_Node.ln_Pri = 51;
  125.  
  126.  
  127. inputRequestBlock->io_Command = IND_ADDHANDLER;
  128. inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  129.  
  130. DoIO((struct IORequest *)inputRequestBlock);
  131.  
  132. for(;;)         /* FOREVER */
  133.       {
  134.       WaitPort( port );
  135.  
  136.       while ((msg = (struct OURMSG *)GetMsg(port)) != NULL)
  137.         {
  138.     Command = msg->Command;
  139.     Misc    = msg->Misc;
  140.     FreeMem(msg, sizeof(struct OURMSG));
  141.     switch(Command)
  142.         {
  143.         case TERMINATE:
  144.             goto abort;
  145.             break;
  146.         
  147.         default:
  148.             break;
  149.         }            
  150.     }
  151.       }
  152.  
  153. abort:
  154.  
  155. if (inputRequestBlock != NULL)
  156.       {
  157.       if (inputRequestBlock->io_Device != NULL)
  158.          {
  159.          inputRequestBlock->io_Command = IND_REMHANDLER;
  160.          inputRequestBlock->io_Data = (APTR)&handlerStuff;
  161.          DoIO((struct IORequest *)inputRequestBlock);
  162.      
  163.          CloseDevice((struct IORequest *)inputRequestBlock);
  164.          }
  165.       DeleteIOReq(inputRequestBlock);
  166.       }
  167. if (inputDevPort != NULL)       DeletePort(inputDevPort);
  168. if (port != NULL)               DeletePort(port);
  169. }
  170.  
  171. void MemCleanup(){}
  172.  
  173. struct MsgPort *CreatePort(name, pri)
  174. char *name;
  175. int pri;
  176. {
  177.    UBYTE sigbit;
  178.    register struct MsgPort *port;
  179.  
  180.    if ((sigbit = AllocSignal(-1)) == -1)
  181.       return((struct MsgPort *)0);
  182.  
  183.    if ((port = (struct MsgPort *)AllocMem(sizeof(struct MsgPort),
  184.                         MEMF_CLEAR|MEMF_PUBLIC)) == 0)
  185.       {
  186.       FreeSignal(sigbit);
  187.       return((struct MsgPort *) (0));
  188.       }
  189.    port->mp_Node.ln_Name = name;
  190.    port->mp_Node.ln_Pri = pri;
  191.    port->mp_Node.ln_Type = NT_MSGPORT;
  192.    port->mp_Flags = PA_SIGNAL;
  193.    port->mp_SigBit = sigbit;
  194.    port->mp_SigTask = (struct Task *)FindTask(0);
  195.    AddPort(port);
  196.    return(port);
  197. }
  198.  
  199. void DeletePort(port)
  200. struct MsgPort *port;
  201. {
  202. RemPort(port);
  203. FreeSignal(port->mp_SigBit);
  204. FreeMem((char *)port,sizeof(struct MsgPort));
  205. }
  206.  
  207. struct IOStdReq *
  208. CreateIOReq(port, size)
  209. struct MsgPort *port;
  210. int size;
  211. {
  212.    struct IOStdReq *ioReq;
  213.  
  214.    if ((ioReq = (struct IOStdReq *)
  215.                 AllocMem(size, MEMF_CLEAR | MEMF_PUBLIC)) != NULL)
  216.       {
  217.       ioReq->io_Message.mn_Node.ln_Type = NT_MESSAGE;
  218.       ioReq->io_Message.mn_Node.ln_Pri  = 0;
  219.       ioReq->io_Message.mn_Length       = size;
  220.       ioReq->io_Message.mn_ReplyPort    = port;
  221.       }
  222.    return(ioReq);
  223. }
  224.  
  225. void DeleteIOReq(ioReq)
  226. struct IOStdReq *ioReq;
  227. {
  228. ioReq->io_Message.mn_Node.ln_Type = 0xff;
  229. ioReq->io_Device = (struct Device *) -1;
  230. ioReq->io_Unit = (struct Unit *) -1;
  231.  
  232. FreeMem( (char *)ioReq, ioReq->io_Message.mn_Length);
  233. }
  234.  
  235.